//
//  WzzCenterController.m
//  Wb_oc
//
//  Created by wzz on 16/7/28.
//  Copyright © 2016年 wzz. All rights reserved.
//

#import "WzzCenterController.h"
#import "UIImage+Context.h"
#import "UIImage+ImageEffects.h"
#import <Masonry/Masonry.h>
#import "WzzCenterZdyBut.h"
#import "ComposeModel.h"

@interface WzzCenterController ()
@property (nonatomic , strong) UIImageView * bgImageView;
@property (nonatomic , strong) UIImageView * backImageView;
@property (nonatomic , strong) UIImageView * logoImageView;
@property (nonatomic , strong) NSArray<ComposeModel *> * dataArray;
@property (nonatomic , strong) NSArray<WzzCenterZdyBut *> * buttons;
@end


@implementation WzzCenterController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

//初始化控件
- (void)setupUI{
    [self.view addSubview:self.bgImageView];
    [self.bgImageView addSubview:self.backImageView];
    [self.bgImageView addSubview:self.logoImageView];
    [self addChildBut];
    
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.bottom.trailing.equalTo(self.view);
    }];
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.bottom.trailing.equalTo(self.bgImageView);
    }];
    [self.logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(100);
    }];

}

- (void)addChildBut{
    // 按钮的宽和高
    CGFloat childW = 80;
    CGFloat childH = 110;
    // 按钮间距
    CGFloat childMargin  = (self.view.bounds.size.width - childW*3)/4;
    

    NSMutableArray * tempArray = [NSMutableArray array];
    [self.dataArray enumerateObjectsUsingBlock:^(ComposeModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        CGFloat col = idx%3;
        CGFloat row = idx/3;

        WzzCenterZdyBut * myBut = [[WzzCenterZdyBut alloc]init];
        [myBut setImage:[UIImage imageNamed:obj.icon] forState:UIControlStateNormal];
        [myBut setTitle:obj.title forState:UIControlStateNormal];
        myBut.frame = CGRectMake(childMargin + (childW+childMargin)*col, (childH+childMargin)*row + 400, childW, childH);
        [self.bgImageView addSubview:myBut];
        [tempArray addObject:myBut];
        
        
    }];;
    self.buttons = tempArray.copy;

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self dismissViewControllerAnimated:self.bgImageView completion:nil];
}



//懒加载
- (UIImageView *)bgImageView{
    if (_bgImageView == nil) {
        _bgImageView = [[UIImageView alloc]init];
        [_bgImageView setImage:[UIImage imageNamed:@"new_feature_2"]];
    }
    return _bgImageView;
}

- (UIImageView *)backImageView{
    if (_backImageView == nil) {
        _backImageView = [[UIImageView alloc]init];
        [_backImageView setImage:[[UIImage getScreenShot] applyDarkEffect]];
    }
    return _backImageView;
}

- (UIImageView *)logoImageView{
    if (_logoImageView == nil) {
        _logoImageView = [[UIImageView alloc]init];
        [_logoImageView setImage:[UIImage imageNamed:@"compose_slogan"]];
    }
    return _logoImageView;
}


- (NSArray<ComposeModel *> *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [ComposeModel appsList];
    }
    return _dataArray;
}


@end
